package memorphic.slide
{
	import mx.core.UIComponent;
	
	
	/**
	 * Adapts a UIComponent-based view so that it can be used as a state view in Slide. Typically, if
	 * you are using an MXML view that cannot extend SlideView (e.g. a Panel, Window or Application)
	 * then you would use composition to add Slide features, rather than inheritence. In that case,
	 * you need to tell the composed SlideView instance which parent container it should update when
	 * the state changes.
	 */
	public class UIComponentStateAdapter implements ISlideStateClient
	{
		
		private var component:UIComponent;
		
		
		public static function apply(component:UIComponent):ISlideStateClient
		{
			return new UIComponentStateAdapter(component);
		}
		
			
		public function UIComponentStateAdapter(component:UIComponent)
		{
			this.component = component;
		}
			
		public function get currentState():String
		{
			return component.currentState;
		}
		public function set currentState(state:String):void
		{
			component.currentState = state;
		}
		
		

		public function get currentViewState():ViewState
		{
			var states:Array = slideStates;
			var n:int = states.length;
			for(var i:int=0; i<n ;i++){
				if(ViewState(states[i]).name == currentState){
					return states[i] as ViewState;
				}
			}
			return null;
		}
		
		
		public function get slideStates():Array
		{
			var states:Array = new Array();
			var n:int = component.states.length;
			for(var i:int=0; i<n; i++){
				if(component.states[i] is ViewState){
					states.push(component.states[i]);
				}
			}
			return states;
		}
		
	}
}